var assert = require('chai').assert;
var request = require('request');
var config = require('../app/config/config.js');

// var showDebug = true;
var debug = (typeof showDebug !== 'undefined') ? console.log.bind(console) : function() {};

var httpBaseUrl = 'http://localhost:' + config.http.port + config.http.baseUrl;
suite('test api/v1/user', function() {
	var __url = httpBaseUrl + '/v1/user';
	// debug(__url);

	var aUser = {
		email: 'aa@bb.com',
		name: 'wzq',
		password: 'abc123',
		phone: '13144025450'
	};
	var query1 = {
		email: 'aa@bb.com',
		password: 'abc123'
	};
	var query2 = {
		email: 'aa@bb.com',
		password: 'dddddddddd'
	};

	test('[x] create user', function(done) {
		request({
			method: 'POST',
			url: __url,
			body: aUser,
			json: true,
			headers: {
				"content-type": "application/json"
			}
		}, function(err, res, body) { // body is object
			debug(body);
			assert.equal(res.statusCode, 200);
			var code = body.errCode;
			assert.isTrue(code === 0 || code === 2004 || code === 2005 || code === 2006); // new or exists
			done();
		});
	});

	test('[x] login', function(done) {
		request({
			method: 'POST',
			url: __url + '/verify',
			body: query1,
			json: true,
			headers: {
				"content-type": "application/json"
			}
		}, function(err, res, body) { // body is object
			assert.equal(res.statusCode, 200);
			assert.equal(body.errCode, 0);
			done();
		});
	});


	test('[x] login2', function(done) {
		request({
			method: 'POST',
			url: __url + '/verify',
			body: query2,
			json: true,
			headers: {
				"content-type": "application/json"
			}
		}, function(err, res, body) { // body is object
			debug(body);
			assert.equal(res.statusCode, 200);
			assert.equal(body.errCode, 2001);
			done();
		});
	});
});